<template>
    <el-dialog :close-on-click-modal="false"  ref="dialogform" title="调整占比" :visible.sync="dialogVisible" width="40%" append-to-body
        :before-close="handleClose">
        <div>
            <div class="flex">
                <div class="label">占比：</div>
                <div class="content">
                    <el-input type="number" v-model="input" placeholder="请输入占比">
                        <template slot="append">%</template>
                    </el-input>
                </div>
                <div class="batchName">批次名称：20231016批次</div>
                <el-button type="text" @click="cancel">提交修改</el-button>
            </div>
            <div class="flex">
                <div class="label">占比：</div>
                <div class="content">
                    <el-input type="number" v-model="input" placeholder="请输入占比">
                        <template slot="append">%</template>
                    </el-input>
                </div>
                <div class="batchName">批次名称：20231016批次</div>
                <el-button type="text" @click="cancel">提交修改</el-button>
            </div>
            <div class="flex">
                <div class="label">占比：</div>
                <div class="content">
                    <el-input type="number" v-model="input" placeholder="请输入占比">
                        <template slot="append">%</template>
                    </el-input>
                </div>
                <div class="batchName">批次名称：20231016批次</div>
                <el-button type="text" @click="cancel">提交修改</el-button>
            </div>
            <div class="flex">
                <div class="label">剩余占比：</div>
                <div>
                    <el-input type="number" disabled v-model="input" placeholder="请输入占比">
                        <template slot="append">%</template>
                    </el-input>
                </div>
            </div>
        </div>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        }
    },
    data () {
        let validatorNumber = function (rule, value, callback) {
            if (value > 0 && value <= 100) {
                let lastTwoDecimal = String(value).split('.');
                if (lastTwoDecimal.length == 2 && lastTwoDecimal[1].length > 2) {
                    callback(new Error('请保留小数点后两位！'));
                } else {
                    callback();
                }
            } else {
                callback(new Error('请输入大于0小于等于100的数！'));
            }
        }
        return {
            addEditName: '添加',
            adjustFlag: false,
            addEditApi: '',
            ruleForm: {},
            input: ''
        }
    },
    computed: {

    },
    watch: {
        dialogVisible (v) {
            v && this.initData()
        }
    },
    
    mounted () { },
    methods: {
        initData () {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                this.addEditApi = ''
                this.addEditName = '添加'
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data }
                this.addEditApi = ''
                this.addEditName = '编辑'
            }
        },
        adjustProportion () {
            this.adjustFlag = true
        },
        //提交表单
        submitForm () {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi.then(res => {
                        this.cancel()
                    }).catch(err => {

                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm () {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel () {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose (done) {
            this.$confirm('确认关闭？')
                .then(_ => {
                    this.resetForm();
                    this.$emit("closeDialog")
                })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped>
.flex {
    display: flex;
    align-items: center;
    margin-bottom: 15px;

    .label,
    .batchName,
    .content {
        margin-right: 10px;
    }

    .label {
        font-weight: bold;
    }
}
</style>
  